/*
 * MachineList.java
 *
 * Created on 19 de julio de 2007, 04:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ws;

import java.util.Vector;
import org.w3c.dom.Document;

/**
 *
 * @author mariano
 */
public class MachineList extends xml.Xmlizable{
    private Vector<Machine> machines;
    /**
     * Creates a new instance of MachineList
     */
    public MachineList() {
        setMachines(new Vector<Machine>());
    }
    
    public MachineList( Vector<Machine> machines ) {
        setMachines( machines );
    }

    public Vector<Machine> getMachines() {
        return machines;
    }

    public void setMachines(Vector<Machine> machines) {
        this.machines = machines;
    }
    
    public void addMachine( Machine machine )
    {
        this.machines.add( machine );
    }
    
    public xml.Coleccion toXmlColeccion()
    {
        xml.Coleccion col = new xml.Coleccion( "machines" );
        
        for( Machine machine:machines )
        {
            col.addItem( machine.toXmlColeccion() );
        }
        
        return col;
    }
    
    public static MachineList fromXml( String str )
    {
        xml.Item col = xml.Item.fromXml( str );
        return parse( col );
    }
    
    public static MachineList fromXml( Document doc )
    {
        xml.Item col = xml.Item.fromXml( doc.getFirstChild() );
        return parse( col );
    }
    
    private static MachineList parse( xml.Item col )
    {
        if( !col.isColeccion() || col.getName() != "machines" )
            return null;
        
        Vector<Machine> vMachines = new Vector<Machine>();
        xml.Coleccion c = (xml.Coleccion)col;
        
        for( int i = 0 ; i < c.size() ; i++ )
        {
            xml.Coleccion cMachine = c.getColeccion( i, "machine" );
            
            if( cMachine != null )
            {
                Machine m = Machine.fromXml( cMachine.toString() );
                if( m != null )
                    vMachines.add( m );
            }
                
        }
        
        return new MachineList( vMachines );
    }
    
}
